-- DieAction
-- create by zouyb
-- 死亡动作

DieAction = class("DieAction", function()
    return ActionBase.create();
end);
function DieAction.create(source, target, skillId)
    local self = DieAction.new();
    self.source = source;
    self.target = target;
    self.skillId = skillId;
    return self;
end

-- 构造函数
function DieAction:ctor()
end

-- 开始播放
function DieAction:play()
    self.state = ActionState.PLAYING;

    if self.target == ME.user then
        -- 玩家死亡
        delay = self:heroDie(self.source, self.target);
    elseif self.target.type == OBJECT_TYPE_MONSTER then
        -- 怪物死亡
        delay = self:monsterDie(self.source, self.target, self.skillId);
    elseif self.target.type == OBJECT_TYPE_BINE then
        -- 魔藤死亡
        delay = self:onBineDie(self.source, self.target);
    elseif self.target.type == OBJECT_TYPE_SUMMON then
        -- 召唤兽死亡
        delay = self:onSummonDie(self.source, self.target);
    elseif self.target.type == OBJECT_TYPE_GHOST_SHIP then
        -- 幽灵船死亡
        delay = self:onShipDie(self.source, self.target);
    end

    self.state = ActionState.END;
    return 0;
end

-- 怪物死亡
function DieAction:monsterDie(source, target, skillId)
    local grids = UIDungeonMgr:getCurLevel():getAllGrids();
    local pos = target:getOwner();
    local grid = grids[pos];

    -- 怪物死亡回调
    local function monsterDieFun()
        local function doDie()
            grid:onMonsterDie();
            EventMgr.fire(event.MONSTER_DIE, { ["pos"] = target:getOwner(), ["skillId"] = skillId, ["source"] = source});
            return 0.3;
        end

        grid.actionManager:addAction(grid.gridData, doDie, "doDie", {}, GRID_ACTION_PHASE_MONSTER_DIED);
    end

    local duration = 0.3;

    -- 刷新起效次数
    if type(target.damageTimes) == "number" then
        if target.damageTimes <= 0 then
            target.delayDamageEffect = false;
        else
            target.damageTimes = target.damageTimes - 1;
        end
    end 

    -- 格挡延迟
    if target.delayDamageEffect then
        local delayTime = iif(type(target.delayDamageEffect) == "number", target.delayDamageEffect, 0.5);   
        local function delay()
            return delayTime;
        end
        grid.actionManager:addAction(grid.gridData, delay, "doDieDelay", {}, GRID_ACTION_PHASE_MONSTER); 
    end

    monsterDieFun();
    return duration;
end

-- 英雄死亡
function DieAction:heroDie(source, target)
    -- 任务： 413
    -- 死亡事件暂时搬到User中
    -- EventMgr.fire(event.HERO_DIE);
    return 0;
end

-- 魔藤死亡
function DieAction:onBineDie(source, target)
    return 0;
end

-- 召唤兽死亡
function DieAction:onSummonDie(source, target)
    local grids = UIDungeonMgr:getCurLevel():getAllGrids();
    local pos = target:getOwner();
    grids[pos]:onSummonDie();

    return 0;
end

-- 幽灵船死亡
function DieAction:onShipDie(source, target)
    local grids = UIDungeonMgr.getCurLevel():getAllGrids();
    local pos = target:getPos();
    grids[pos]:onShipDie();

    return 0;
end


return DieAction;